<?php
namespace School\Controller;
use Think\Cache\Driver\Memcache;
use Think\Controller;

class TestController extends Controller{
    public function add(){
        $data=array();
        $data['name']='石俊波';
        $data['sex']='男';
        $data['class']= '电子';
        $data['school']='厦门大学';

       $number=rand(10000000,99999999);

       //检查学号是否被占用
//        $s_id = M('member')->where('number = %d',$number)->getField('id'); //方法1  %d 意思是将$number强制转成整形，前提得是该字段在数据库也是整型

        $where['number'] = $number; //按学号查询
        $s_id = M('member')->where($where)->getField('id'); //方法2 ， 以数组的形式查询，这个方法很常用



        if($s_id){
            //已被占用
            dump('已被占用');
        }else{
            $data['number']="$number";
            $School=M('test');  //哪来的test表
            $id= $School->add($data);
            if($id){
                echo "操作成功，ID为：".$id;
            }
            else {
//                dump($id);
//                dump($School->getLastSql());
//                dump($School->getError());
                echo "操作失败！！！";
            }
        }


    }
        public function find1(){
        $user=M('test');
        $data=$user->where('name="朱迪雅"')->select();
        dump($data);
        }
        public function find2(){
        $user=M('test');
        $data=$user->where('sex="女"')->select();
        dump($data);
        }
    public function find3()
    {
        $user = M('test');
        $data = $user->where('number="43157958"')->select();
        dump($data);
    }
    public function find4(){
        $user=M('test');
        $data=$user->where('time="2018-07-20 13:48:36"')->select();
        dump($data);
    }
}